﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.COM;
using MINERP.DAL;
using MINERP.MODEL.FoundationModel;

namespace MINERP.BLL.InfoConfig
{
    public class ShippingBLL
    {
        public  List<ShippingCompany_Info> GetAllShippingCompany()
        {
             List<ShippingCompany_Info> rtnList=new List<ShippingCompany_Info>();
            int useable = (int) ShippingCompanyStatus.Normal;
            using (newSampleYPDBEntities db=new newSampleYPDBEntities())
            {
                List<ShippingCompany> list = db.ShippingCompany.Where(p => p.Status == useable).ToList();
                foreach (ShippingCompany shippingCompany in list)
                {
                    ShippingCompany_Info info=new ShippingCompany_Info(){Alias = shippingCompany.Alias,ShippingCompanyName = shippingCompany.ShippingCompanyName,ShippingFormatPath = shippingCompany.ShippingFormatPath,Status = shippingCompany.Status,TrackingUrl = shippingCompany.Tracking_url,ShippingCompanyID = shippingCompany.ShippingCompanyID};
                    rtnList.Add(info);
                }
            }
            return rtnList;

        }
        public int AddOrUpdateShipping(ShippingCompany_Info inInfo)
        {
            int rtnInt = 0;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                ShippingCompany shippingCompany =
                    db.ShippingCompany.FirstOrDefault(p => p.ShippingCompanyID == inInfo.ShippingCompanyID);
                if (shippingCompany == null)
                {
                   ShippingCompany shipping=new ShippingCompany();
                   shipping.ShippingCompanyName = inInfo.ShippingCompanyName;
                   shipping.ShippingFormatPath = inInfo.ShippingFormatPath;
                   shipping.Tracking_url = inInfo.TrackingUrl;
                   shipping.Alias = inInfo.Alias;
                   shipping.Status = (int) ShippingCompanyStatus.Normal;
                    rtnInt = 0;
                    db.AddToShippingCompany(shipping);
                }
                else
                {
                    shippingCompany.ShippingCompanyName = inInfo.ShippingCompanyName;
                    shippingCompany.ShippingFormatPath = inInfo.ShippingFormatPath;
                    shippingCompany.Tracking_url = inInfo.TrackingUrl;
                    shippingCompany.Alias = inInfo.Alias;
                    rtnInt = 1;
                }
                db.SaveChanges();
                return rtnInt;
            }
        }
        public int UpdateShipping(ShippingCompany_Info inInfo)
        {
            using (newSampleYPDBEntities db=new newSampleYPDBEntities())
            {
                ShippingCompany shippingCompany =
                    db.ShippingCompany.FirstOrDefault(p => p.ShippingCompanyID == inInfo.ShippingCompanyID);
                if (shippingCompany==null)
                {
                    return 0;
                }
                else
                {
                    shippingCompany.ShippingCompanyName = inInfo.ShippingCompanyName;
                    shippingCompany.ShippingFormatPath = inInfo.ShippingFormatPath;
                    shippingCompany.Tracking_url = inInfo.TrackingUrl;
                    shippingCompany.Alias = inInfo.Alias;
                }
                db.SaveChanges();
                return 1;
            }
        }
        public int DelShippingCompany(ShippingCompany_Info inInfo)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                ShippingCompany shippingCompany =
                    db.ShippingCompany.FirstOrDefault(p => p.ShippingCompanyID == inInfo.ShippingCompanyID);
                if (shippingCompany == null)
                {
                    return 0;
                }
                else
                {
                    shippingCompany.Status = (int)ShippingCompanyStatus.delete;
                    db.SaveChanges();
                    return 1;
                }


            }
        }
    }
}
